package com.yupi.springbootinit.controller;

import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.DevOrProdType;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.product.ProductRequest;
import com.yupi.springbootinit.model.entity.Product;
import com.yupi.springbootinit.model.vo.ProductVO;
import com.yupi.springbootinit.model.vo.UserVO;
import com.yupi.springbootinit.service.ProductService;
import com.yupi.springbootinit.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
@CrossOrigin(origins = {DevOrProdType.devString},allowCredentials = "true")

public class ProductController {

    @Resource
    private ProductService productService;

    @Resource
    private UserService userService;

    /**
     * 展示所有商品
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<ProductVO>> ListAllProduct(ProductRequest productRequest){
        List<ProductVO> list = productService.listAllProduct(productRequest);
        return ResultUtils.success(list);
    }

    /**
     * 创建商品
     * @param product
     * @param request
     * @return
     */
    @PostMapping("/create")
    public BaseResponse<Long> CreateProduct(@RequestBody Product product,HttpServletRequest request){
        if(product == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserVO loginUser = userService.getLoginUser(request);
        Long result = productService.createProduct(product,loginUser);
        return ResultUtils.success(result);
    }

    /**
     * 更新商品信息
     * @param productVO
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> UpdateProduct(@RequestBody ProductVO productVO,HttpServletRequest request){
        if(productVO == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        boolean result = productService.updateProduct(productVO,request);
        return ResultUtils.success(result);
    }

    /**
     * 展示我创建的商品
     * @param request
     * @return
     */
    @GetMapping("/show/my")
    public BaseResponse<List<ProductVO>> ShowMyProduct(HttpServletRequest request){
        UserVO loginUser = userService.getLoginUser(request);
        List<ProductVO> list = productService.showMyProduct(loginUser);
        return ResultUtils.success(list);
    }

    @GetMapping("/select")
    public BaseResponse<ProductVO> ShowSelectProduct(Long id){
        Product product = productService.getById(id);
        if (product == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        return ResultUtils.success(productVO);
    }


}
